 /*
 * debug.c
 *
 *  Created on: 2012-6-8
 *      Author: Administrator
 */

#include "tangoc.h"

void show_raw(char axis, int len, int start, short int raw[], char flag, int fptr)
{
	int i;
	char buf[1024] = "\0";
	sprintf(buf, "%c: ", axis);
	for (i = 0; i < len; i++)
	{
		if (raw[i + start] > 999) raw[i + start] = 999;
		else if (raw[i + start] < -999) raw[i + start] = -999;
		char str[8];
		sprintf(str, "%4d ", raw[i + start]);
		strcat(buf, str);
	}
	strcat(buf, "\n");
	printf("%s", buf);

	if ((flag == 'Y' || flag == 'y') && len > 0)
	{
		write(fptr, buf, strlen(buf));
	}
}

void tango1(int type, int mode, char flag)
{
	int i, j, ret, fptr = -1;
	unsigned char xline = 0;
	unsigned char yline = 0;
	unsigned char buf[256], buf1[256], buf2[256];
	short int xyraw[128];
	volatile int RAW_ADDR=0x8E;


	active_power();
	ioctl(gdev, DISABLE_IRQ, 0);
	for (i = 0; i < 3; i++)
	{
		getXYN(buf, 2);
		xline = buf[0];
		yline = buf[1];
		if (xline == 0 || yline == 0 || xline > 48 || yline > 48) continue;
		else break;
	}
	printf("xline=%d yline=%d\n", xline, yline);
	if (xline == 0 || yline == 0 || xline > 48 || yline > 48)
	{
		puts("read scan lines error.");
		ioctl(gdev, ENABLE_IRQ, 0);
		restore_power_mode();
		return;
	}

	if (flag == 'Y' || flag == 'y')
	{
		char fdat[32] = "";
		get_time(mode, fdat);
		fptr = open(fdat, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
		if (fptr == -1)
		{
			puts("Open raw.dat failed.");
			ioctl(gdev, ENABLE_IRQ, 0);
			restore_power_mode();
			return;
		}
	}

	//set_mode(mode, 0);
	init_keyboard();
	while (1)
	{
		if (kbhit())
		{
			char key = getkey();
			//getchar();
			if (key == '\n')
			{
				enable_algo();
				ioctl(gdev, RESET_TP, 0);
				msleep(100);
				ioctl(gdev, ENABLE_IRQ, 0);
				restore_power_mode();
				break;
			}
		}
			
		for(j=0; j<(((xline + yline - 2)%3) ? (1+((xline + yline - 2)/3)):(((xline + yline - 2)/3))); j++) {
		//printf(">>>%d,%d<<<<\n", j, ((((xline + yline - 2)/3))));					
		set_mode(mode, 0);
		ret = read_raw(buf1, 6, (RAW_ADDR+6*j));//(xline + yline - 2) * 2);
		for (i = 0; i < 3; i++)//xline + yline - 2; i++)
		{
			//xyraw[i+j*6] = 0;
			xyraw[i+j*3] = ((buf1[i * 2 + 1] << 8) | buf1[i * 2]);
			//printf("%4d ",xyraw[i+j*3]);
		}
		//printf("\n");
		//msleep(500);
		}
		show_raw('X', xline - 1, 0, xyraw, flag, fptr);
		show_raw('Y', yline - 1, xline - 1, xyraw, flag, fptr);
	}
	close_keyboard();
	if (flag == 'Y' || flag == 'y')
	{
		close(fptr);
	}
}

void tango2(int type, int mode, char flag)
{
	int i, ret, len, fptr = -1;
	unsigned char xline = 0;
	unsigned char yline = 0;
	unsigned char xline2 = 0;
	unsigned char buf[131];
	short int xyraw1[34], xyraw2[32], xy_tmp[66];

	if (flag == 'Y' || flag == 'y')
	{
		char fdat[32] = "";
		get_time(mode, fdat);
		fptr = open(fdat, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
		if (fptr == -1)
		{
			puts("Open raw.dat failed.");
			return;
		}
	}

	init_keyboard();
	ioctl(gdev, DISABLE_IRQ, 0);
	ioctl(gdev, mode, 2);
	while (1)
	{
		if (kbhit())
		{
			char key = getkey();
			if (key == '\n')
			{
				ioctl(gdev, ENABLE_IRQ, 0);
				break;
			}
		}
		ret = read(gdev, buf, 131);
		xline = buf[64];
		yline = buf[65];
		xline2 = buf[66]; // printf("xline=%d yline=%d xline2=%d\n", xline, yline, xline2);
		for (i = 0; i < 32; i++)
		{
			xyraw1[i] = ((buf[i * 2 + 1] << 8) | buf[i * 2]);
			xyraw2[i] = ((buf[67 + i * 2 + 1] << 8) | buf[67 + i * 2]);
		}

		for (i = 0; i < xline - 1; i++)
		{
			xy_tmp[i] = xyraw2[i];
		}
		if (xline2)
		{
			for (i = 0; i < xline2 - 1; i++)
			{
				xy_tmp[xline - 1 + i] = xyraw1[i];
			}
			len = xline - 1 + xline2 - 1;
		}
		else
		{
			len = xline - 1;
		}
		show_raw('X', len, 0, xy_tmp, flag, fptr);
		show_raw('Y', yline - 1, xline2, xyraw1, flag, fptr);
	}
	close_keyboard();
	if (flag == 'Y' || flag == 'y')
	{
		ioctl(gdev, DEFAULT, 0);
		close(fptr);
	}
}

void debug_set(int type, int mode, char flag)
{
	switch (type)
	{
	case 1:
	case 3:
		tango1(type, mode, flag);
		break;
	case 2:
	case 4:
	case 5:
		tango2(type, mode, flag);
		break;
	}
}

void debug_menu()
{
	puts("\nDebug menu, press a key in brackes.");
	puts("[N] No offset");
	puts("[R] RAM Offset");
	puts("[E] EEPROM offset");
	puts("[B] Back main menu");
	puts("[X] Exit");
}

void input_debug_param(char *flag)
{
	printf("Do you save the data?(Y/N):");
	while (1)
	{
		*flag = getchar();
		if (*flag == 'y' || *flag == 'Y' || *flag == 'n' || *flag == 'N') break;
	}
	while (getchar() != '\n');
}

void debug_mode()
{
	char cmd, flag = 'n';
	int type = 1;
	unsigned char loop = 1;

	debug_menu();
	while(loop)
	{
		printf("Input cmd: ");
		cmd = getchar();
		while (getchar() != '\n');
		switch(cmd)
		{
		case 'n':
		case 'N':
			input_debug_param(&flag);
			debug_set(type, DEBUG_ORIGIN_MODE, flag);
			break;
		case 'r':
		case 'R':
			input_debug_param(&flag);
			debug_set(type, DEBUG_RAM_OFFSET, flag);
			break;
		case 'e':
		case 'E':
			input_debug_param(&flag);
			debug_set(type, DEBUG_EEPROM_OFFSET, flag);
			break;
		case 'b':
		case 'B':
			loop = 0;
			main_menu();
			break;
		case 'x':
		case 'X':
			finish();
			break;
		}
		debug_menu();
	}
}
